package Day010_排序算法_异常.ZuoYe;

public class Test4 {
    public static void main(String[] args) {

        //创建对象
        Student stu = new Student();
        try {
            stu.setSex("女");

        } catch (SexException e) {
            System.out.println(e.getMessage());
        }
        try {
            stu.setAge(10);
        } catch (AgeException e) {
            System.out.println(e.getMessage());
        }
    }
}

//年龄异常类
class AgeException extends RuntimeException {
    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }
}

//性别异常类
class SexException extends RuntimeException {
    public SexException() {
    }

    public SexException(String message) {
        super(message);
    }
}

//Student类
class Student {
    private String name;
    private String sex;
    private int age;

    public Student() {
    }

    public Student(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        if (sex.equals("男") != true && sex.equals("女") != true) {
            throw new SexException("性别异常");
        }
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 50 || age < 15) {
            throw new AgeException("年龄异常");
        }
        this.age = age;
    }
}